<template>
  <div class="about-container">
    <div class="about-card">
      <div class="app-logo">
        <div class="logo-placeholder">
          <span class="logo-icon">🚀</span>
        </div>
      </div>

      <div class="app-info">
        <h1>Electron多功能工具</h1>
        <div class="version">版本 v1.0.0</div>
        <p class="description">
          一个基于Electron和Vue
          3的跨平台多功能工具软件，提供了现代化、酷炫的用户界面，包含文件管理、地图功能、应用设置等多种功能。
        </p>

        <div class="system-info">
          <div class="info-item">
            <div class="info-label">Electron版本</div>
            <div class="info-value" id="electron-version">-</div>
          </div>
          <div class="info-item">
            <div class="info-label">Chrome版本</div>
            <div class="info-value" id="chrome-version">-</div>
          </div>
          <div class="info-item">
            <div class="info-label">Node版本</div>
            <div class="info-value" id="node-version">-</div>
          </div>
          <div class="info-item">
            <div class="info-label">操作系统</div>
            <div class="info-value">{{ platform }}</div>
          </div>
        </div>
      </div>
    </div>

    <div class="action-section">
      <div class="actions">
        <button class="action-btn" @click="checkForUpdates">
          <span class="btn-icon">🔄</span>
          检查更新
        </button>
        <button class="action-btn" @click="openHomepage">
          <span class="btn-icon">🌐</span>
          访问主页
        </button>
        <button class="action-btn" @click="openLicenses">
          <span class="btn-icon">📜</span>
          查看许可证
        </button>
      </div>
    </div>

    <div class="credits-section">
      <h2>贡献者</h2>
      <div class="contributors">
        <div class="contributor">
          <div class="contributor-avatar">👨‍💻</div>
          <div class="contributor-info">
            <div class="contributor-name">开发团队</div>
            <div class="contributor-role">主要开发者</div>
          </div>
        </div>
        <div class="contributor">
          <div class="contributor-avatar">🎨</div>
          <div class="contributor-info">
            <div class="contributor-name">设计团队</div>
            <div class="contributor-role">UI/UX设计</div>
          </div>
        </div>
        <div class="contributor">
          <div class="contributor-avatar">🔍</div>
          <div class="contributor-info">
            <div class="contributor-name">测试团队</div>
            <div class="contributor-role">质量测试</div>
          </div>
        </div>
      </div>
    </div>

    <div class="license-section" v-if="showLicenses">
      <h2>开源许可证</h2>
      <div class="license-text">
        <h3>MIT License</h3>
        <p>Copyright (c) 2023 Electron多功能工具</p>
        <p>
          Permission is hereby granted, free of charge, to any person obtaining
          a copy of this software and associated documentation files (the
          "Software"), to deal in the Software without restriction, including
          without limitation the rights to use, copy, modify, merge, publish,
          distribute, sublicense, and/or sell copies of the Software, and to
          permit persons to whom the Software is furnished to do so, subject to
          the following conditions:
        </p>
        <p>
          The above copyright notice and this permission notice shall be
          included in all copies or substantial portions of the Software.
        </p>
        <p>
          THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
          EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
          MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
          IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
          CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
          TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
          SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        </p>
        <button class="close-btn" @click="closeLicenses">关闭</button>
      </div>
    </div>

    <div class="update-section" v-if="updateStatus !== 'none'">
      <div class="update-notification" :class="updateStatus">
        <div class="update-icon">
          {{
            updateStatus === "checking"
              ? "🔍"
              : updateStatus === "available"
              ? "🎉"
              : "✅"
          }}
        </div>
        <div class="update-message">
          <strong>{{ updateStatusMessage }}</strong>
          <p v-if="updateStatus === 'available'">
            新版本 v{{ newVersion }} 已可用，建议更新。
          </p>
        </div>
        <div class="update-actions" v-if="updateStatus === 'available'">
          <button class="update-btn" @click="installUpdate">立即更新</button>
          <button class="update-cancel" @click="dismissUpdate">稍后再说</button>
        </div>
        <div class="update-spinner" v-if="updateStatus === 'checking'"></div>
        <button
          class="update-close"
          v-if="updateStatus !== 'checking'"
          @click="dismissUpdate"
        >
          ×
        </button>
      </div>
    </div>

    <div class="footer">
      <p>© 2023 Electron多功能工具. 保留所有权利.</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";

// 显示平台信息
const platform = ref(formatPlatform(window.electronAPI?.platform || ""));

// 许可证显示状态
const showLicenses = ref(false);

// 更新状态: 'none' | 'checking' | 'available' | 'not-available'
const updateStatus = ref("none");
const updateStatusMessage = ref("");
const newVersion = ref("0.0.0");

// 检查更新
const checkForUpdates = () => {
  updateStatus.value = "checking";
  updateStatusMessage.value = "正在检查更新...";

  // 模拟检查更新的流程
  setTimeout(() => {
    // 在实际应用中，这里应该调用Electron的自动更新API
    const hasUpdate = Math.random() > 0.5; // 模拟随机是否有更新

    if (hasUpdate) {
      updateStatus.value = "available";
      updateStatusMessage.value = "发现新版本!";
      newVersion.value = "1.1.0";
    } else {
      updateStatus.value = "not-available";
      updateStatusMessage.value = "您的应用已是最新版本";

      // 3秒后自动关闭提示
      setTimeout(() => {
        if (updateStatus.value === "not-available") {
          updateStatus.value = "none";
        }
      }, 3000);
    }
  }, 2000);
};

// 安装更新
const installUpdate = () => {
  // 在实际应用中，这里应该调用Electron的自动更新API来安装更新
  alert("正在下载并安装更新，应用将在安装完成后重启。");
  updateStatus.value = "none";
};

// 关闭更新提示
const dismissUpdate = () => {
  updateStatus.value = "none";
};

// 打开主页
const openHomepage = () => {
  // 在实际应用中，这里应该调用Electron的shell.openExternal API
  window.electronAPI?.fileOperation?.({
    action: "openExternal",
    url: "https://example.com",
  });
};

// 打开许可证
const openLicenses = () => {
  showLicenses.value = true;
};

// 关闭许可证
const closeLicenses = () => {
  showLicenses.value = false;
};

// 格式化平台名称
const formatPlatform = (platform: string): string => {
  switch (platform) {
    case "win32":
      return "Windows";
    case "darwin":
      return "macOS";
    case "linux":
      return "Linux";
    default:
      return platform || "未知";
  }
};

onMounted(() => {
  // 在元素中填充版本信息
  const electronVersionElement = document.getElementById("electron-version");
  const chromeVersionElement = document.getElementById("chrome-version");
  const nodeVersionElement = document.getElementById("node-version");

  if (electronVersionElement)
    electronVersionElement.textContent = process.versions.electron || "-";
  if (chromeVersionElement)
    chromeVersionElement.textContent = process.versions.chrome || "-";
  if (nodeVersionElement)
    nodeVersionElement.textContent = process.versions.node || "-";
});
</script>

<style lang="scss" scoped>
.about-container {
  padding: 30px;
  max-width: 800px;
  margin: 0 auto;
}

.about-card {
  display: flex;
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
  margin-bottom: 30px;
}

.app-logo {
  margin-right: 30px;

  .logo-placeholder {
    width: 120px;
    height: 120px;
    background: var(--primary-gradient);
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;

    .logo-icon {
      font-size: 4rem;
    }
  }
}

.app-info {
  flex: 1;

  h1 {
    font-size: 2rem;
    margin: 0 0 5px;
    color: var(--dark-color);
  }

  .version {
    font-size: 1rem;
    color: var(--primary-color);
    margin-bottom: 15px;
  }

  .description {
    font-size: 1rem;
    color: var(--gray-color);
    line-height: 1.6;
    margin-bottom: 20px;
  }
}

.system-info {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
  background: #f8f9fa;
  padding: 15px;
  border-radius: 8px;

  .info-item {
    .info-label {
      font-size: 0.8rem;
      color: var(--gray-color);
      margin-bottom: 5px;
    }

    .info-value {
      font-size: 0.9rem;
      color: var(--dark-color);
      font-weight: 500;
    }
  }
}

.action-section {
  margin-bottom: 30px;

  .actions {
    display: flex;
    gap: 10px;

    .action-btn {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 12px 20px;
      background: white;
      border: 1px solid #e1e5eb;
      border-radius: 8px;
      color: var(--dark-color);
      font-size: 0.9rem;
      cursor: pointer;
      transition: all 0.2s ease;

      &:hover {
        background: #f8f9fa;
        border-color: var(--primary-color);
        color: var(--primary-color);
      }

      .btn-icon {
        margin-right: 10px;
        font-size: 1.2rem;
      }
    }
  }
}

.credits-section {
  background: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
  margin-bottom: 30px;

  h2 {
    font-size: 1.5rem;
    margin: 0 0 20px;
    color: var(--dark-color);
  }

  .contributors {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
  }

  .contributor {
    display: flex;
    align-items: center;

    .contributor-avatar {
      width: 50px;
      height: 50px;
      background: #f0f2f5;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 15px;
      font-size: 1.5rem;
    }

    .contributor-info {
      .contributor-name {
        font-size: 1rem;
        color: var(--dark-color);
        font-weight: 500;
        margin-bottom: 2px;
      }

      .contributor-role {
        font-size: 0.8rem;
        color: var(--gray-color);
      }
    }
  }
}

.license-section {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;

  .license-text {
    width: 80%;
    max-width: 700px;
    max-height: 80vh;
    background: white;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 5px 30px rgba(0, 0, 0, 0.2);
    overflow-y: auto;

    h3 {
      font-size: 1.5rem;
      margin: 0 0 20px;
      color: var(--dark-color);
    }

    p {
      font-size: 0.9rem;
      color: var(--gray-color);
      line-height: 1.6;
      margin-bottom: 15px;
    }

    .close-btn {
      display: block;
      margin: 20px auto 0;
      padding: 10px 20px;
      background: var(--primary-color);
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;

      &:hover {
        opacity: 0.9;
      }
    }
  }
}

.update-section {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 90;

  .update-notification {
    display: flex;
    align-items: center;
    background: white;
    padding: 15px 20px;
    border-radius: 8px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
    min-width: 300px;
    max-width: 400px;
    position: relative;

    &.checking {
      border-left: 4px solid #ffa502;
    }

    &.available {
      border-left: 4px solid #2ed573;
    }

    &.not-available {
      border-left: 4px solid #7bed9f;
    }

    .update-icon {
      font-size: 1.5rem;
      margin-right: 15px;
    }

    .update-message {
      flex: 1;

      strong {
        display: block;
        font-size: 1rem;
        color: var(--dark-color);
        margin-bottom: 5px;
      }

      p {
        font-size: 0.8rem;
        color: var(--gray-color);
        margin: 0;
      }
    }

    .update-actions {
      display: flex;
      gap: 10px;
      margin-top: 10px;

      .update-btn {
        padding: 6px 12px;
        background: var(--primary-color);
        color: white;
        border: none;
        border-radius: 4px;
        font-size: 0.8rem;
        cursor: pointer;

        &:hover {
          opacity: 0.9;
        }
      }

      .update-cancel {
        padding: 6px 12px;
        background: none;
        color: var(--gray-color);
        border: none;
        font-size: 0.8rem;
        cursor: pointer;

        &:hover {
          color: var(--dark-color);
        }
      }
    }

    .update-spinner {
      width: 20px;
      height: 20px;
      border: 2px solid rgba(0, 0, 0, 0.1);
      border-top-color: var(--primary-color);
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }

    .update-close {
      position: absolute;
      top: 10px;
      right: 10px;
      background: none;
      border: none;
      font-size: 1.2rem;
      color: var(--gray-color);
      cursor: pointer;

      &:hover {
        color: var(--dark-color);
      }
    }
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.footer {
  text-align: center;
  font-size: 0.9rem;
  color: var(--gray-color);
  margin-top: 20px;
}
</style>
